package com.a918.service.impl;

import com.a918.dao.*;
import com.a918.entity.*;
import com.a918.service.UserService;
import com.a918.util.PasswordHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImp implements UserService {

    private UserMapper userMapper;
    private User2roleMapper user2roleMapper;
    private RoleMapper roleMapper;
    private Role2permissionMapper role2permissionMapper;
    private PermissionMapper permissionMapper;
    private MemBwgMapper memBwgMapper;
    private MemPerMapper memPerMapper;
    private MemOthMapper memOthMapper;
    private UserlogMapper userlogMapper;
    private Bwg2wenwuMapper bwg2wenwuMapper;
    private Bwg2governMapper bwg2governMapper;
    private RegisterFileMapper registerFileMapper;
    private FrontFcCheckMapper frontFcCheckMapper;

    @Autowired
    public UserServiceImp(UserMapper userMapper, User2roleMapper user2roleMapper, RoleMapper roleMapper, Role2permissionMapper role2permissionMapper, PermissionMapper permissionMapper, MemBwgMapper memBwgMapper, MemPerMapper memPerMapper, MemOthMapper memOthMapper, UserlogMapper userlogMapper, Bwg2wenwuMapper bwg2wenwuMapper, Bwg2governMapper bwg2governMapper, RegisterFileMapper registerFileMapper, FrontFcCheckMapper frontFcCheckMapper) {
        this.userMapper = userMapper;
        this.user2roleMapper = user2roleMapper;
        this.roleMapper = roleMapper;
        this.role2permissionMapper = role2permissionMapper;
        this.permissionMapper = permissionMapper;
        this.memBwgMapper = memBwgMapper;
        this.memPerMapper = memPerMapper;
        this.memOthMapper = memOthMapper;
        this.userlogMapper = userlogMapper;
        this.bwg2wenwuMapper = bwg2wenwuMapper;
        this.bwg2governMapper = bwg2governMapper;
        this.registerFileMapper = registerFileMapper;
        this.frontFcCheckMapper = frontFcCheckMapper;
    }

    @Override
    public List<Role> findRoles(Integer userid) {
        List<Integer> roleid = user2roleMapper.selectRoleidByUserId(userid);
        List<Role> roles = new ArrayList<>();
        for(Integer i: roleid){
            roles.add(roleMapper.selectByPrimaryKey(i));
        }
        return roles;
    }

    @Override
    public List<String> findPermissions(Integer userid) {
        List<Integer> roleid = user2roleMapper.selectRoleidByUserId(userid);
        Set<Integer> permissionidlist = new HashSet<>();
        List<String> permissionUrl = new ArrayList<>();
        for(Integer i: roleid){
            permissionidlist.addAll(role2permissionMapper.getPermissionIdByRoleId(i));
        }
        for(Integer x: permissionidlist){
            permissionUrl.add(permissionMapper.selectByPrimaryKey(x).getUrl());
        }
        return permissionUrl;
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }

    @Override
    public void insertUser(User user) {
        User userToDB = new PasswordHelper().encryptPassword(user);
        userMapper.insertSelective(userToDB);
    }

    @Override
    public boolean validatePasswd(User user, String oldpassword) {
        String jiamiPassword = new PasswordHelper().jiamiPasswd(oldpassword);
        return user.getPassword().equals(jiamiPassword);
    }

    @Override
    public void updateUser(User user) {
        User userToDB = new PasswordHelper().encryptPassword(user);
        userMapper.updateByPrimaryKey(userToDB);

    }

    @Override
    public List<Permission> getPermissionList() {
        return permissionMapper.getlist();
    }

    @Override
    public List<Role> getRoleList() { return roleMapper.getlist(); }

    @Override
    public void insertRole(Role role) {
        roleMapper.insertSelective(role);
    }

    @Override
    public void delRole(String id) {
        role2permissionMapper.deleteByRoleId(Integer.valueOf(id));
        roleMapper.deleteByPrimaryKey(Integer.valueOf(id));
    }

    //下方两个方法公用这个函数
    private void commonlist(List<Map<String, Object>> res, List<Integer> permissionIdList, Integer id, String name) {
        Map<String,Object> temp = new HashMap<>();
        temp.put("id", id);
        temp.put("name", name);
        if(permissionIdList.contains(id)){
            temp.put("ishave",1);
        }else {
            temp.put("ishave",0);
        }
        res.add(temp);
    }


    @Override
    public List<Map<String,Object>> getPermissionByRole(Integer roleid) {
        List<Integer> permissionIdList = role2permissionMapper.getPermissionIdByRoleId(roleid);
        List<Permission> getlist = permissionMapper.getlist();
        List<Map<String,Object>> res = new ArrayList<>();
        for(Permission i: getlist){
            commonlist(res, permissionIdList, i.getId(), i.getName());
        }

        return res;
    }


    @Override
    public List<Map<String, Object>> getRoleByUser(Integer userid) {
        List<Integer> roleidlist = user2roleMapper.selectRoleidByUserId(userid);
        List<Role> getlist = roleMapper.getlist();
        List<Map<String,Object>> res = new ArrayList<>();
        for(Role i: getlist){
            commonlist(res, roleidlist, i.getId(), i.getName());
        }

        return res;
    }

    @Override
    public void roleManage(String type, Integer roleid, Integer perid) {
        if(type.equals("add")){
            Role2permission role2per = new Role2permission();
            role2per.setRoleid(roleid);
            role2per.setPermissionid(perid);
            role2permissionMapper.insertSelective(role2per);
        }else if(type.equals("del")){
            role2permissionMapper.delByRoleidAndPerid(roleid,perid);

        }

    }

    @Override
    public List<User> getUserList(Map<String, String> paramMap) {
        return userMapper.getList(paramMap);
    }

    @Override
    public void delUser(Integer userId) {
        List<Integer>  idlist = memBwgMapper.getIdListbyUserId(userId);
        for(Integer i:idlist){
            bwg2governMapper.deleteByBwgid(i);
            bwg2wenwuMapper.deleteByBwgid(i);
        }
        memBwgMapper.deleteByUserId(userId);
        memPerMapper.deleteByUserId(userId);
        memOthMapper.deleteByUserId(userId);
        userMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void user2roleManage(String type, Integer roleid, Integer userid) {
        if(type.equals("add")){
            User2role user2role = new User2role();
            user2role.setRoleid(roleid);
            user2role.setUserid(userid);
            user2roleMapper.insertSelective(user2role);
        }else if(type.equals("del")){
            user2roleMapper.delByRoleidAndUserid(roleid,userid);

        }
    }

    @Override
    public void insertlog(String des, String url, String username,String ip) {
            Userlog log = new Userlog();
            log.setUrl(url);
            log.setDes(des);
            log.setUsername(username);
            log.setIp(ip);
        userlogMapper.insertSelective(log);
    }

    @Override
    public List<Userlog> getLogList(Map<String, String> paramMap) {
        return userlogMapper.getLogList(paramMap);
    }

    @Override
    public MemBwgWithBLOBs getBwgWithBLOBsByUserid(Integer userid) {
        return memBwgMapper.getBwgWithBLOBsByUserid(userid).get(0);
    }

    @Override
    public MemPerWithBLOBs getPerWithBLOBsByUserid(Integer userid) {
        return memPerMapper.getgetPerWithBLOBsByUserid(userid).get(0);
    }

    @Override
    public MemOth getOthByUserid(Integer userid) {
        return memOthMapper.getOthListByUserid(userid).get(0);
    }

    @Override
    public void deleteRegisterFileById(Integer id) {
        registerFileMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<RegisterFile> getRegisterFileByUserid(Integer userid) {
        return registerFileMapper.getRegisterFileByUserid(userid);
    }

    @Override
    public void insertRegisterFile(RegisterFile registerFile) {
        registerFileMapper.insertSelective(registerFile);

    }

    @Override
    public void updateCheckstatus(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void insertShenheyijian(FrontFcCheck frontFcCheck) {
        frontFcCheckMapper.insert(frontFcCheck);
    }

    @Override
    public void updateUserCheckstatus(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public String getSHYJByCheckid(Integer userid) {
        String type = "register";
        FrontFcCheck frontFcCheck = frontFcCheckMapper.selectByTypeAndCheckid(userid,type);
        String shenheyijian = frontFcCheck.getOpinion();
        if(shenheyijian==null){
            return "无";
        }
        return shenheyijian;
    }

    @Override
    public void insertUser2Role(User2role user2role) {
        user2roleMapper.insertSelective(user2role);
    }

    @Override
    public PageInfo<User> findUserList_check(Map<String, String> paramMap) {
        int pno=Integer.parseInt(paramMap.get("page"));
        int pageSize=Integer.parseInt(paramMap.get("limit"));
        PageHelper.startPage(pno, pageSize);
        List<User> list = userMapper.findUserList_check();;
        return new PageInfo<User>(list);

    }

}
